package org.cwgl.interfaces.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.cwgl.dao.ClimasDao;
import org.cwgl.dao.StandbookDao;
import org.cwgl.dao.UsercaseDao;
import org.cwgl.exception.MyException;
import org.cwgl.interfaces.BaseDataService;
import org.cwgl.interfaces.SelectService;
import org.cwgl.model.Climas;
import org.cwgl.model.Standbook;
import org.cwgl.model.Usercase;
import org.cwgl.support.Page;
import org.cwgl.util.StringUtil;
import org.cwgl.vo.ClimasVo;
import org.cwgl.vo.Result;
import org.cwgl.vo.StandbookVo;
import org.cwgl.vo.UsercaseVo;
import org.hibernate.Session;

public class BaseDataSerImpl implements BaseDataService
{
	private UsercaseDao usercaseDao;
	private StandbookDao standbookDao;
	private ClimasDao  climasDao;
	private SelectService selectService;
	
	
	
	
	public void setClimasDao(ClimasDao climasDao) {
		this.climasDao = climasDao;
	}

	public void setStandbookDao(StandbookDao standbookDao) {
		this.standbookDao = standbookDao;
	}

	public void setSelectService(SelectService selectService) 
	{
		this.selectService = selectService;
	}

	public void setUsercaseDao(UsercaseDao usercaseDao) 
	{
		this.usercaseDao = usercaseDao;
	}
	/*
	 * (non-Javadoc)   已测试成功   2012-12-17
	 * @see org.cwgl.interfaces.BaseDataService#addUsercase(org.cwgl.vo.UsercaseVo)
	 */
	@Override
	public Result addUsercase(UsercaseVo usercase)
	{
		Result result=new Result();
		result.setSuccess(false);
		if(usercase==null)
		{
			result.setException("案例不能为空!");
			return (result);
		}
		if(!StringUtil.isEmpty(usercase.getNcname()))
		{
			result.setException("案例名不能为空!");
			return (result);
		}	
		if(usercase.getGrossmoney()==null)
		{
			usercase.setGrossmoney(0.0);
		}
		if(!StringUtil.isEmpty(usercase.getIsflag()))
		{
			usercase.setIsflag("1");
		}
		Usercase uc=new Usercase(usercase.getUcid(),usercase.getNcname(),
				usercase.getGrossmoney(),usercase.getIsflag());
		Session session=usercaseDao.getSession();
		if(session==null)
		{
			result.setException("数据连接失败!");
			return (result);
		}
		session.save(uc);
		result.setSuccess(true);
		result.setException("案例插入成功!");
		return (result);
	}
	/*
	 * (non-Javadoc)  已测试成功   2012-12-17
	 * @see org.cwgl.interfaces.BaseDataService#addStandbook(org.cwgl.vo.StandbookVo)
	 */
	@Override
	public Result addStandbook(StandbookVo book)
	{
		Result result=new Result();
		result.setSuccess(false);
		if(book==null)
		{
			result.setException("案例信息不能为空!");
			return (result);
		}
		if(book.getUsercase()==null
			|| !StringUtil.isEmpty(book.getGno())
			|| !StringUtil.isEmpty(book.getParties())
			|| book.getRecorddata()==null)
		{
			result.setException("案例信息不全!");
			return (result);
		}
		if(book.getSalesmoney()==null)
		{
			book.setSalesmoney(0.0);
		}
		if(book.getIncomemoney()==null)
		{
			book.setIncomemoney(0.0);
		}
		if(! StringUtil.isEmpty(book.getIsflag()))
		{
			book.setIsflag("1");
		}
		Standbook sb=new Standbook();
		sb.setGid(book.getGid());
		sb.setUsercase(book.getUsercase());
		sb.setBusinesstype(book.getBusinesstype());
		sb.setIncomemoney(book.getIncomemoney());
		sb.setIsflag(book.getIsflag());
		sb.setMoneytype(book.getMoneytype());
		sb.setParties(book.getParties());
		sb.setReceiptdate(book.getReceiptdate());
		sb.setRecorddata(book.getRecorddata());
		sb.setSalesmoney(book.getSalesmoney());
		sb.setGno(book.getGno());
		sb.setRate(book.getRate());
		Session session=standbookDao.getSession();
		if(session==null)
		{
			result.setException("数据连接失败!");
			return (result);
		}
		session.save(sb);
		result.setSuccess(true);
		result.setException("案例信息插入成功!");
		return (result);
		
		
	}
	/*
	 * (non-Javadoc)  已测试成功     2012-12-17
	 * @see org.cwgl.interfaces.BaseDataService#addClamis(org.cwgl.vo.ClimasVo)
	 */
	@Override
	public Result addClamis(ClimasVo clamis)
	{
		Result result=new Result();
		result.setSuccess(false);
		if(clamis==null)
		{
			result.setException("帐务信息不能为空!");
			return (result);
		}
		if(!StringUtil.isEmpty(clamis.getCname())
		|| !StringUtil.isEmpty(clamis.getCdesc())
		|| !StringUtil.isEmpty(clamis.getProofno())
		|| clamis.getCprice()==null
		|| clamis.getCleandate()==null
		|| clamis.getClosingdate()==null)
		{
			result.setException("输入的帐务信息不能为空");
			return (result);
		}
		if(clamis.getBillnum()==null)
		{
			clamis.setBillnum(0);
		}
		if(!StringUtil.isEmpty(clamis.getIsflag()))
		{
			clamis.setIsflag("1");
		}
		Climas cl=new Climas(clamis.getCid(),clamis.getCname(),clamis.getCdesc(),
				clamis.getCprice(),clamis.getClosingdate(),clamis.getCleandate(),
				clamis.getProofno());
		Session session=climasDao.getSession();
		if(session==null)
		{
			result.setException("数据连接失败!");
			return (result);
		}
		session.save(cl);
		result.setSuccess(true);
		result.setException("已插入一条帐务数据");
		return (result);
	}
	/*
	 * JUnit 测试成功   (2012-12-19)
	 * @see org.cwgl.interfaces.BaseDataService#deleteUsercase(org.cwgl.vo.UsercaseVo)
	 */
	@Override
	public Result deleteUsercase(UsercaseVo usercase) 
	{
		Result result=new Result();
		result.setSuccess(false);
		if(usercase==null)
		{
			result.setException("案例不能为空!");
			return (result);
		}
		if(!StringUtil.isEmpty(usercase.getNcname()))
		{
			result.setException("案例名不能为空!");
			return (result);
		}	
		if(usercase.getIsflag().equals("0"))
		{
			result.setException("此案例已被改过!");
			return (result);
		}
		if(!StringUtil.isEmpty(usercase.getIsflag())
			||	usercase.getIsflag().equals("1"))
		{
			usercase.setIsflag("0");
		}
		
			int uid=usercase.getUcid();
			Page standbooks;
			try {
				standbooks = selectService.findStandbookByUsercaseId(uid,0,999999);
			} catch (MyException e) {
				result.setException("查询相关案例信息时出现异常!");
				return (result);
			}
			if(standbooks==null)
			{
				result.setException("无相关结果");
				return (result);
			}
			List<HashMap<String,Object>> books=standbooks.getResult();

			if(books==null || books.size()==0)
			{
				result.setException("没找到您想要的结果");
				return (result);
			}		
			Usercase uc=new Usercase(usercase.getUcid(),usercase.getNcname(),
					usercase.getGrossmoney(),usercase.getIsflag());	
			for (int i=0; i<books.size(); i++)
			{

				StandbookVo sv=new StandbookVo();
				sv.setGid(Integer.parseInt(books.get(i).get("GID").toString()));
				sv.setGno(books.get(i).get("GNO").toString());
				sv.setBusinesstype(books.get(i).get("BUSINESSTYPE").toString());
				sv.setIncomemoney(Double.valueOf(books.get(i).get("INCOMEMONEY").toString()));
				sv.setIsflag(books.get(i).get("ISFLAG").toString());
				sv.setMoneytype(books.get(i).get("MONEYTYPE").toString());
				sv.setParties(books.get(i).get("PARTIES").toString());
				sv.setRate(Double.valueOf(books.get(i).get("RATE").toString()));
				try {
					sv.setReceiptdate(
					  new SimpleDateFormat("yyyy-MM-dd").parse(books.get(i).get("RECEIPTDATE").toString()));
				} catch (ParseException e) {
				
					result.setException("从数据库中获取登记日期失败!");
					return (result);
				}
				try {
					sv.setRecorddata(
					  new SimpleDateFormat("yyyy-MM-dd").parse(books.get(i).get("RECORDDATA").toString()));
				} catch (ParseException e) {
					
					result.setException("从数据库中获取收款日期失败!");
					return (result);
				}
				sv.setSalesmoney(Double.valueOf(books.get(i).get("SALESMONEY").toString()));
				sv.setUsercase(uc);
				Result r=deleteStandbook(sv);
			}
				
		Session session=usercaseDao.getSession();
		if(session==null)
		{
			result.setException("数据连接失败!");
			return (result);
		}
		session.merge(uc);
		result.setSuccess(true);
		result.setException("案例及相关案例信息删除成功!");
		return (result);
		
	}
	/*
	 * (non-Javadoc)  JUnit测试OK (2012-12-18)
	 * @see org.cwgl.interfaces.BaseDataService#deleteStandbook(org.cwgl.vo.StandbookVo)
	 */
	@Override
	public Result deleteStandbook(StandbookVo book)
	{
		Result result=new Result();
		result.setSuccess(false);
		if(book==null)
		{
			result.setException("请指定要删除的案例信息!");
			return (result);
		}
		Standbook b;
		try {
			b = selectService.findStandbookById(book.getGid());
		} catch (MyException e) {
			result.setException("查询您想要删除案例信息时出现异常!");
			return (result);
		}
		if(b==null)
		{
			result.setException("没找到您要删除的案例信息!");
			return (result);
		}
		if(b.getIsflag().equals("0"))
		{
			result.setException("此条案例信息已经被删过了!");
			return (result);
		}
		b.setIsflag("0");
		Session session=standbookDao.getSession();
		if(session==null)
		{
			result.setException("数据连接失败!");
			return (result);
		}
		session.saveOrUpdate(b);
		result.setSuccess(true);
		result.setException("已成功删除此条案例信息");
		return (result);
	}
	/*
	 * (non-Javadoc)  JUnit测试成功     (2012-12-18)
	 * @see org.cwgl.interfaces.BaseDataService#deleteClamis(org.cwgl.vo.ClimasVo)
	 */
	@Override
	public Result deleteClamis(ClimasVo clamis)
	{
		Result result=new Result();
		result.setSuccess(false);
		if(clamis==null)
		{
			result.setException("请指定要删除的帐务信息!");
			return (result);
		}
		Climas c;
		try {
			c = selectService.findClimasById(clamis.getCid());
		} catch (MyException e) {
			result.setException("查询您想要删除帐务信息时出现异常!");
			return (result);
		}
		if(c==null)
		{
			result.setException("没找到您要删除的帐务信息!");
			return (result);
		}
		if(c.getIsflag().equals("0"))
		{
			result.setException("您已经删过此条帐务信息了!");
			return (result);
		}
		c.setIsflag("0");
		Session session=climasDao.getSession();
		if(session==null)
		{
			result.setException("数据连接失败!");
			return (result);
		}
		session.saveOrUpdate(c);
		result.setSuccess(true);
		result.setException("已成功删除此条帐务");
		return (result);
	}
	/*
	 * (non-Javadoc)   JUnit测试成功    (2012-12-18)
	 * @see org.cwgl.interfaces.BaseDataService#updateUsercase(org.cwgl.vo.UsercaseVo)
	 */
	@Override
	public Result updateUsercase(UsercaseVo usercase) 
	{
		Result result=new Result();
		result.setSuccess(false);
		if(usercase==null)
		{
			result.setException("更新的案例不能为空!");
			return (result);
		}
		if(! StringUtil.isEmpty(usercase.getNcname()))
		{
			result.setException("案例名不能为空!");
			return (result);
		}
		if(!StringUtil.isEmpty(usercase.getIsflag()))
		{
			usercase.setIsflag("1");
		}
		Usercase u;
		try {
			u = selectService.findUsercaseById(usercase.getUcid());
		} catch (MyException e) {
			result.setException("查询您要更新案例时出现异常!");
			return (result);
		}
		if(u==null)
		{
			result.setException("您要更新的案例不存在!");
			return (result);
		}
		u.setNcname(usercase.getNcname());
		u.setGrossmoney(usercase.getGrossmoney());
		u.setIsflag(usercase.getIsflag());
		Session session=usercaseDao.getSession();
		if(session==null)
		{
			result.setException("数据连接失败!");
			return (result);
		}
		session.update(u);
		result.setSuccess(true);
		result.setException("已成功更新案例!");
		return (result);
	}
	/*
	 * (non-Javadoc)    JUnit测试成功    (2012-12-18)
	 * @see org.cwgl.interfaces.BaseDataService#updateStandbook(org.cwgl.vo.StandbookVo)
	 */
	@Override
	public Result updateStandbook(StandbookVo standbook) 
	{
		Result result=new Result();
		result.setSuccess(false);
		if(standbook==null)
		{
			result.setException("需要更新的信息不能为空");
			return (result);
		}	
		
		if(!StringUtil.isEmpty(standbook.getGno()))
		{
			result.setException("不能将编号更新为空!");
			return (result);
		}
		if(!StringUtil.isEmpty(standbook.getParties()))
		{
			result.setException("不能将签订方更新为空!");
			return (result);
		}
		if(standbook.getRecorddata()==null)
		{
			result.setException("不能将登记日期更新为空!");
			return (result);
		}
		if(standbook.getIncomemoney()==null)
		{
			standbook.setIncomemoney(0.0);
		}
		if(standbook.getSalesmoney()==null)
		{
			standbook.setSalesmoney(0.0);
		}
		if(standbook.getRate()==null)
		{
			standbook.setRate(0.17);
		}
		if(!StringUtil.isEmpty(standbook.getMoneytype()))
		{
			standbook.setMoneytype("0");
		}
		if(!StringUtil.isEmpty(standbook.getBusinesstype()))
		{
			standbook.setBusinesstype("1");
		}
		if(!StringUtil.isEmpty(standbook.getIsflag()))
		{
			standbook.setIsflag("1");
		}
		Usercase u;
		try {
			u = selectService.findUsercaseById(standbook.getUsercase().getUcid());
		} catch (MyException e) {
			result.setException("查询与更新的案例信息相关的案例时出现异常!");
			return (result);
		}
		if(u==null)
		{
			result.setException("找不到与更新的案例信息相关的案例!");
			return (result);
		}
		Standbook sb;
		try {
			sb = selectService.findStandbookById(standbook.getGid());
		} catch (MyException e) {
			result.setException("查询您想更新的案例信息时出现异常!");
			return (result);
		}
		sb.setUsercase(u);
		sb.setGno(standbook.getGno());
		sb.setParties(standbook.getParties());
		sb.setRecorddata(standbook.getRecorddata());
		sb.setReceiptdate(standbook.getReceiptdate());
		sb.setSalesmoney(standbook.getSalesmoney());
		sb.setIncomemoney(standbook.getIncomemoney());
		sb.setMoneytype(standbook.getMoneytype());
		sb.setRate(standbook.getRate());
		sb.setBusinesstype(standbook.getBusinesstype());
		sb.setIsflag(standbook.getIsflag());
		Session session=standbookDao.getSession();
		if(session==null)
		{
			result.setException("数据连接失败!");
			return (result);
		}
		session.update(sb);
		result.setSuccess(true);
		result.setException("已成功更新一条案例信息");
		return (result);
	}
	/*
	 * (non-Javadoc)   JUnit测试成功     (2012-12-18)
	 * @see org.cwgl.interfaces.BaseDataService#updateClimas(org.cwgl.vo.ClimasVo)
	 */
	@Override
	public Result updateClimas(ClimasVo climas)
	{
		Result result=new Result();
		result.setSuccess(false);
		if(climas==null)
		{
			result.setException("需要更新的帐务不能为空!");
			return (result);
		}
		
		if(!StringUtil.isEmpty(climas.getCname()))
		{
			result.setException("帐务申报人不能为空!");
			return (result);
		}
		if(!StringUtil.isEmpty(climas.getCdesc()))
		{
			result.setException("内容描述不能为空!");
			return (result);
		}
		if(!StringUtil.isEmpty(climas.getCprice().toString()))
		{
			result.setException("申报金额不能为空!");
			return (result);
		}
		if(climas.getClosingdate()==null)
		{
			result.setException("申报时间不能为空!");
			return (result);
		}
		if(climas.getCleandate()==null)
		{
			result.setException("清算时间不能为空!");
			return (result);
		}
		if(!StringUtil.isEmpty(climas.getProofno()))
		{
			result.setException("凭证号码不能为空!");
			return (result);
		}
		if(!StringUtil.isEmpty(climas.getIsflag()))
		{
			climas.setIsflag("1");
		}
		Climas c;
		try {
			c = selectService.findClimasById(climas.getCid());
		} catch (MyException e) {
			result.setException("查询您要更新的帐务时出现异常!");
			return (result);
		}
		if(c==null)
		{
			result.setException("没找到您要更新的帐务!");
			return (result);
		}
		c.setCname(climas.getCname());
		c.setCdesc(climas.getCdesc());
		c.setCprice(climas.getCprice());
		c.setClosingdate(climas.getClosingdate());
		c.setCleandate(climas.getCleandate());
		c.setProofno(climas.getProofno());
		c.setRemark(climas.getRemark());
		c.setBillnum(climas.getBillnum());
		c.setIsflag(climas.getIsflag());
		Session session=usercaseDao.getSession();
		if(session==null)
		{
			result.setException("数据连接失败!");
			return (result);
		}
		session.update(c);
		result.setSuccess(true);
		result.setException("已成功更新一条帐务数据");
		return (result);
	}

}
